var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    userId: '',
    packerId: '',
    phone: '',
    name: '',
    plateNumber: '',
    cardNumber: '',
    idNumber: '',
    bankNumber: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    var phone = options.phone
    wx.showLoading({ title: '加载中' })
    wx.request({
      method: 'POST',
      url: app.preUrl + 'getSupplierByQueryInfo',
      dataType: 'json',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        queryInfo: phone, //查询信息(输入姓名或者手机号码)
        packerId: app.globalData.userId,
        page: 1, //这里初始值为1
        size: 8
      },
      success: function(res) {
        wx.hideLoading()
        if (res.data.code == 100) {
          let data = res.data.result[0]
          console.log(data)
          that.setData({
            userId: data.userId || '',
            phone: data.phone || '',
            name: data.name || '',
            plateNumber: data.plateNumber || '', //车牌号
            idNumber: data.idNumber || '', //身份证号
            cardNumber: data.cardNumber || '', //ic卡号
            bankNumber: data.bankNumber || '' //银行卡号
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.message,
            confirmColor: '#56BDE0',
            showCancel: false
          })
        }
      },
      fail: function(error) {}
    })
  },
  //银行卡号前端校验
  onbankCardNameEvent: function(e) {
    var that = this
    that.setData({
      bankNumber: e.detail.value.replace(/\D/g, '')
    })
  },
  //手机号前端校验
  onPhoneEvent: function(e) {
    var that = this
    if (e.detail.value.length == 1) {
      that.setData({
        phone: e.detail.value.replace(/[^1-9]/g, '')
      })
    } else {
      that.setData({
        phone: e.detail.value.replace(/\D/g, '')
      })
    }
  },
  bindName(e) {
    this.setData({
      name: e.detail.value
    })
  },
  bindPlateNumber(e) {
    this.setData({
      plateNumber: e.detail.value
    })
  },
  bindCardNumber(e) {
    this.setData({
      cardNumber: e.detail.value
    })
  },
  bindIdNumber(e) {
    this.setData({
      idNumber: e.detail.value
    })
  },
  bindBankNumber(e) {
    this.setData({
      bankNumber: e.detail.value
    })
  },
  bindFormSubmit() {
    var that = this
    var phone = that.data.phone
    var name = that.data.name
    var plateNumber = that.data.plateNumber
    var cardNumber = that.data.cardNumber
    var idNumber = that.data.idNumber
    var bankNumber = that.data.bankNumber

    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/
    if (!phone) {
      wx.showToast({
        title: '手机号必填',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!myreg.test(phone)) {
      wx.showToast({
        title: '手机号有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!name) {
      wx.showToast({
        title: '客户姓名必填',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (!plateNumber) {
      wx.showToast({
        title: '车牌号必填',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (
      (cardNumber.length && cardNumber.length < 6) ||
      (cardNumber.length && cardNumber.length > 18)
    ) {
      wx.showToast({
        title: 'IC卡号输入错误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (idNumber.length && idNumber.length != 18) {
      wx.showToast({
        title: '身份证长度有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else if (
      bankNumber.length &&
      bankNumber.length != 16 &&
      bankNumber.length != 17 &&
      bankNumber.length != 18 &&
      bankNumber.length != 19
    ) {
      wx.showToast({
        title: '银行卡长度有误',
        icon: 'none',
        duration: 1500
      })
      return false
    } else {
      that.setData({ buthiddenSubmit: true })
      wx.showLoading({ title: '提交中' })
      wx.request({
        method: 'POST',
        url: app.preUrl + 'updateSupplierUser',
        dataType: 'json',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: that.data.userId,
          packerId: app.globalData.userId, //打包站ID
          phone: phone,
          name: name,
          plateNumber: plateNumber, //车牌号
          idNumber: idNumber, //身份证号
          cardNumber: cardNumber, //ic卡号
          bankNumber: bankNumber //银行卡号
        },
        success: function(res) {
          wx.hideLoading()
          if (res.data.code == 100) {
            wx.showToast({
              title: '修改成功',
              icon: 'success',
              duration: 1000
            })

            setTimeout(function() {
              wx.navigateBack({
                delta: 1
              })
            }, 1500)
          } else {
            wx.showModal({
              title: '提示',
              content: res.data.message,
              confirmColor: '#56BDE0',
              showCancel: false
            })
            that.setData({
              buthiddenSubmit: false
            })
          }
        },
        fail: function(error) {
          that.setData({
            buthiddenSubmit: false
          })
        }
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {}
})
